const net = require("net");

const server = net.createServer();
const clientList = [];

//处理客户端连接请求
server.on("connection", (client) => {
  console.log("come a client");
  clientList.push(client);

  //处理客户端发送的消息
  client.on("data", (msg) => {
    console.log("receive a msg:" + msg.toString());
    clientList.forEach((element) => {
      //发送给其它客户端
      if (element != client) {
        element.write(msg);
      }
    });
  });

  client.on("error", (error) => {
    console.log(error);
    clientList.pop(client);
  });
});

//开始监听
console.log("start listen 9091");
server.listen(9091);
